package main

import (
	"fmt"

	"github.com/spf13/viper"
)

type MysqlConfig struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
}

type RedisConfig struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
}

type Database struct {
	Mysql MysqlConfig `mapstructure:"mysql"`
	Redis RedisConfig `mapstructure:"redis"`
}

func main() {
	// 1.实例化一个viper
	v := viper.New()
	// 2.设置文件路径
	v.SetConfigFile("./config-dev.yaml")
	// 3.读取文件
	err := v.ReadInConfig()
	if err != nil {
		panic(err)
	}

	// 4.读取成字符串
	fmt.Println("Host", v.Get("Host"))
	fmt.Println("Port", v.Get("Port"))
	fmt.Println("Unknown", v.Get("Unknown"))

	// 5.读取成struct
	db := Database{}
	_ = v.Unmarshal(&db)
	fmt.Println("db", db)
}
